package com.ruicar.afs.cloud.channel.online.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.channel.channelworkflow.entity.WorkTaskApproveRecord;
import com.ruicar.afs.cloud.channel.channelworkflow.entity.WorkTaskAssign;
import com.ruicar.afs.cloud.channel.channelworkflow.entity.WorkTaskDetail;
import com.ruicar.afs.cloud.channel.channelworkflow.service.WorkTaskApproveRecordService;
import com.ruicar.afs.cloud.channel.channelworkflow.service.WorkTaskAssignService;
import com.ruicar.afs.cloud.channel.channelworkflow.service.WorkTaskDetailService;
import com.ruicar.afs.cloud.channel.common.condition.BaseInfoTypeDTO;
import com.ruicar.afs.cloud.channel.common.condition.StartOrStop;
import com.ruicar.afs.cloud.channel.common.condition.StartOrStopDTO;
import com.ruicar.afs.cloud.channel.common.until.ChannelConfig;
import com.ruicar.afs.cloud.channel.common.until.ChannelRoleEnum;
import com.ruicar.afs.cloud.channel.common.until.RoleTypeDic;
import com.ruicar.afs.cloud.channel.online.condition.*;
import com.ruicar.afs.cloud.channel.online.entity.ChannelBaseInfoTemp;
import com.ruicar.afs.cloud.channel.online.entity.ChannelMainBrand;
import com.ruicar.afs.cloud.channel.online.entity.ChannelQuotaInfoTemp;
import com.ruicar.afs.cloud.channel.online.feign.ApplyFeignService;
import com.ruicar.afs.cloud.channel.online.feign.CaseFeignService;
import com.ruicar.afs.cloud.channel.online.feign.VoucherFlowApiFeign;
import com.ruicar.afs.cloud.channel.online.mapper.ChannelOnlineMapper;
import com.ruicar.afs.cloud.channel.online.service.ChannelMainBrandService;
import com.ruicar.afs.cloud.channel.online.service.ChannelOnlineService;
import com.ruicar.afs.cloud.channel.online.vo.ChannelBulkEditingQuotaInfoVo;
import com.ruicar.afs.cloud.channel.online.vo.ChannelOnlineInfoVo;
import com.ruicar.afs.cloud.channel.online.vo.ChannelOnlineInterFaceVo;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.uid.AfsSequenceGenerator;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.dto.mq.voucher.VoucherFlowInfoDto;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.ruicar.afs.cloud.channel.online.untils.ChannelOnlineConstants.*;

/**
 * @author mingzhi.li
 * @version 1.0
 * @describe 渠道上线申请
 * @date 2020/05/19 11：20
 */
@Service
@AllArgsConstructor
@Slf4j
public class ChannelOnlineServiceImpl extends ServiceImpl<ChannelOnlineMapper, ChannelBaseInfoTemp> implements ChannelOnlineService {

    @Autowired
    private final ApplyFeignService applyFeignService;
    @Autowired
    private final CaseFeignService caseFeignService;
    @Autowired
    private ChannelConfig channelConfig;
    @Autowired
    private VoucherFlowApiFeign voucherFlowApiFeign;
    private final WorkTaskAssignService workTaskAssignService;
    private final WorkTaskDetailService workTaskDetailService;
    private final WorkTaskApproveRecordService workTaskApproveRecordService;
    private final ChannelMainBrandService channelMainBrandService;
    /**
     * 判断返回的序号长度
     */
    static final Integer LENGTH1 = 1;
    static final Integer LENGTH2 = 2;
    static final Integer LENGTH3 = 3;
    static final Integer LENGTH4 = 4;
    static final Integer LENGTH5 = 5;
    static final Integer LENGTH6 = 6;

    /**
     * 序列号
     */
    @Autowired
    private AfsSequenceGenerator afsSequenceGenerator;



    /**
     * 渠道代码自定义生成生成处理
     *
     * @param keyType
     * @param typeIndex
     * @return
     */
    @SuppressWarnings("AlibabaUndefineMagicConstant")
    @Override
    public String channelCodeSequence(String keyType, String typeIndex) {
        String channelCode = "";
        Long lastValue = afsSequenceGenerator.genNext(keyType, typeIndex);
        if (lastValue != null) {
            int length = lastValue.toString().length();
            if (length == LENGTH1) {
                channelCode = "00000" + lastValue;
            } else if (length == LENGTH2) {
                channelCode = "0000" + lastValue;
            } else if (length == LENGTH3) {
                channelCode = "000" + lastValue;
            } else if (length == LENGTH4) {
                channelCode = "00" + lastValue;
            } else if (length == LENGTH5) {
                channelCode = "0" + lastValue;
            } else if (length >= LENGTH6) {
                channelCode = lastValue.toString();
            }
        }
        return "GWMFC-DP-" + channelCode;
    }

    /**
     * 多条件查询渠道列表
     *
     * @param page
     * @param condition
     * @return
     */
    @Override
    public IPage<ChannelOnlineInfoVo> getChannelInfo(Page page, ChannelOnlineCondition condition) {
        return baseMapper.getChannelInfo(page, condition);
    }

    /**
     * 多条件查询渠道展区区域
     *
     * @param page
     * @param condition
     * @return
     */
    @Override
    public IPage<List<ChannelOnlineInfoVo>> getAreaListCondition(Page page, ChannelOnlineCondition condition) {
        return baseMapper.getAreaListInfo(page, condition);
    }

    /**
     * 创建账号
     *
     * @param dto
     * @return
     */

    @Override
    public IResponse createByParentId(ChannelBasicTempCondition dto) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", channelConfig.getApplyAdminClientId());
        requestHeader.put("clientSecret", channelConfig.getApplyAdminClientSecret());
        IResponse resultMsg = applyFeignService.createByParentId(dto, requestHeader);
        return IResponse.success(resultMsg);
    }

    /**
     * 同步授权区域到进件授权子系统
     *
     * @param condition
     * @return
     */

    @Override
    public IResponse synchronousAuthRegion(ChannelAuthRegionCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", channelConfig.getApplyAdminClientId());
        requestHeader.put("clientSecret", channelConfig.getApplyAdminClientSecret());
        IResponse resultMsg = applyFeignService.synchronousAuthRegion(condition, requestHeader);
        return IResponse.success(resultMsg);
    }


    /**
     * 同步数据到案件服务
     *
     * @param vo
     */
    @Override
    public IResponse synchronousDataToCase(ChannelOnlineInterFaceVo vo) {
        //请求头
        Map<String, String> headers = new HashMap<>();
        //接口返回结果
        headers.put("clientId", channelConfig.getCaseClientId());
        headers.put("clientSecret", channelConfig.getCaseClientSecret());
        IResponse result = (IResponse) caseFeignService.synchronousDataToCase(vo, headers);
        //结果解析只有成功时，方可返回正确的值
        return IResponse.success(result);
    }

    /**
     * 渠道额度占用/释放
     *
     * @param temp
     */
    @Override
    public IResponse limitTakeUpOrLease(ChannelQuotaInfoTemp temp) {
        //请求头
        Map<String, String> headers = new HashMap<>();
        //接口返回结果
        headers.put("clientId", channelConfig.getCaseClientId());
        headers.put("clientSecret", channelConfig.getCaseClientSecret());
        IResponse result = (IResponse) caseFeignService.limitTakeUpOrLease(temp, headers);
        //结果解析只有成功时，方可返回正确的值
        return IResponse.success(result);
    }

    /**
     * 根据channelId获取部门树
     *
     * @param channelId
     * @return
     */

    @Override
    public IResponse getDeptInfo(String channelId, String parentId) {
        Map<String, String> requestHeader = new HashMap<>();
        Map<String, String> requestParam = new HashMap<>();
        requestParam.put("channelId", channelId);
        requestParam.put("parentId", parentId);
        requestHeader.put("clientId", channelConfig.getApplyAdminClientId());
        requestHeader.put("clientSecret", channelConfig.getApplyAdminClientSecret());
        return applyFeignService.getDeptInfo(requestParam, requestHeader);
    }
    /**
     * 根据id重置密码
     *
     * @param id
     * @return
     */

    @Override
    public IResponse resetPassword(Long id) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", channelConfig.getApplyAdminClientId());
        requestHeader.put("clientSecret", channelConfig.getApplyAdminClientSecret());
        return applyFeignService.resetPassword(id, requestHeader);
    }

    /**
     * 获取系统用户信息
     *
     * @param condition
     * @return
     */

    @Override
    public IResponse getUserInfo(ChannelUserCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", channelConfig.getApplyAdminClientId());
        requestHeader.put("clientSecret", channelConfig.getApplyAdminClientSecret());
        return applyFeignService.getUserInfo(condition, requestHeader);
    }

    /**
     * 根据分页获取所有角色
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */

    @Override
    public IResponse getAllByPage(int pageNumber, int pageSize,String roleType) {
        Map<String, String> requestHeader = new HashMap<>();
        Map<String, Object> requestParam = new HashMap<>();
        requestParam.put("pageSize", pageSize);
        requestParam.put("pageNumber", pageNumber);
        requestParam.put("roleType", roleType);
        requestHeader.put("clientId", channelConfig.getApplyAdminClientId());
        requestHeader.put("clientSecret", channelConfig.getApplyAdminClientSecret());
        return applyFeignService.getAllRoleByRoleType(requestParam, requestHeader);
    }

    /**
     * 批量修改经纬度范围同步案件服务
     *
     * @param tempList
     */
    @Override
    public IResponse synchronousLlRangeToCase(List<ChannelBaseInfoTemp> tempList) {
        //请求头
        Map<String, String> headers = new HashMap<>();
        //接口返回结果
        headers.put("clientId", channelConfig.getCaseClientId());
        headers.put("clientSecret", channelConfig.getCaseClientSecret());
        IResponse result = (IResponse) caseFeignService.synchronousLlRangeToCase(tempList, headers);
        //结果解析只有成功时，方可返回正确的值
        return IResponse.success(result);
    }

    /**
     * 批量修改区域经理
     *
     * @param channelOnlineCondition
     */
    @Override
    public IResponse synchronousLlManagerToCase(ChannelOnlineCondition channelOnlineCondition) {
        //请求头
        Map<String, String> headers = new HashMap<>();
        //接口返回结果
        headers.put("clientId", channelConfig.getCaseClientId());
        headers.put("clientSecret", channelConfig.getCaseClientSecret());
        IResponse result = (IResponse) caseFeignService.synchronousLlManagerToCase(channelOnlineCondition, headers);
        //结果解析只有成功时，方可返回正确的值
        return IResponse.success(result);
    }
    /**  地域树获取合作商信息*/
    @Override
    public IPage<List<ChannelOnlineInfoVo>> getByRegion(Page page, RegionDTO condition,List<String> codes,List<Long> channelIds) {
        return baseMapper.getByRegion(page, condition,codes,channelIds);
    }

    /**  渠道发展树获取合作商信息*/
    @Override
    public IPage<List<ChannelOnlineInfoVo>> getByDevelop(Page page, DevelopDTO condition,List<Long> ids ) {
        return baseMapper.getByDevelop(page, condition,ids);
    }

    @Override
    public List<ChannelBaseInfoTemp> getRoleChannelList() {
            List<Long> ids =new ArrayList<>();
            //  businessType：01是新车  02 二手车  03是非直营新车二手车都有
            // ownBusinessType ：01是新车  02 二手车  03  是直营新车二手车都有

            // 第一步我要知道登录者的角色属性  是属于直营还是非直营
            List<String> role = SecurityUtils.getRoles();
            BaseInfoTypeDTO dto =new BaseInfoTypeDTO();
            // 先判断类型
            if(role.contains(ChannelRoleEnum.ROLE_ADMIN.name())){
                // 说明进来的是管理员
                dto.setBusinessType(RoleTypeDic.ALL_CAR);
                dto.setOwnBusinessType(RoleTypeDic.ALL_CAR);

            }else {
                if(role.contains(ChannelRoleEnum.ROLE_OTHER_NEW_MANAGE_CHANNEL_DEVELOP.name())||role.contains(ChannelRoleEnum.ROLE_OTHER_NEW_SEE_CHANNEL_DEVELOP.name())){
                    // 进入这个说明有非直营的新车角色
                    dto.setBusinessType(RoleTypeDic.NEW_CAR);
                }
                if(role.contains(ChannelRoleEnum.ROLE_OTHER_OLD_MANAGE_CHANNEL_DEVELOP.name())||role.contains(ChannelRoleEnum.ROLE_OTHER_OLD_SEE_CHANNEL_DEVELOP.name())){
                    if(RoleTypeDic.NEW_CAR.equals(dto.getBusinessType())){
                        // 说明我之前新车 又有二手车
                        dto.setBusinessType(RoleTypeDic.ALL_CAR);
                    }else {
                        // 说明我只有二手车
                        dto.setBusinessType(RoleTypeDic.OLD_CAR);
                    }
                }
                // 再去看看他有直营的角色吗
                if(role.contains(ChannelRoleEnum.ROLE_OWN_NEW_MANAGE_CHANNEL_DEVELOP.name())||role.contains(ChannelRoleEnum.ROLE_OWN_NEW_SEE_CHANNEL_DEVELOP.name())){
                    dto.setOwnBusinessType(RoleTypeDic.NEW_CAR);
                }
                if(role.contains(ChannelRoleEnum.ROLE_OWN_OLD_MANAGE_CHANNEL_DEVELOP.name())||role.contains(ChannelRoleEnum.ROLE_OWN_OLD_SEE_CHANNEL_DEVELOP.name())){
                    if(RoleTypeDic.NEW_CAR.equals(dto.getOwnBusinessType())){
                        // 说明我之前新车 又有二手车
                        dto.setOwnBusinessType(RoleTypeDic.ALL_CAR);
                    }else {
                        // 说明我只有二手车
                        dto.setOwnBusinessType(RoleTypeDic.OLD_CAR);
                    }
                }
            }

            // 再去查询自己可以查看的合作商id
            List<ChannelBaseInfoTemp> list =new ArrayList<>();
            // 先判断是不是直营和非直营都有
            if(dto.getBusinessType()!=null&&dto.getBusinessType()!=""&&dto.getOwnBusinessType()!=null&&dto.getOwnBusinessType()!=""){
                // 进入这里就说明  既可以做直营也可以做非直营
                if(RoleTypeDic.ALL_CAR.equals(dto.getBusinessType())&&RoleTypeDic.ALL_CAR.equals(dto.getOwnBusinessType())){
                    // 进入这里说明直营非直营  新车二手车都能做
                    list =this.getBaseInfoByStatus(dto);
                    return list;
                }
                if(RoleTypeDic.ALL_CAR.equals(dto.getBusinessType())&&!RoleTypeDic.ALL_CAR.equals(dto.getOwnBusinessType())){
                    //  说明  非直营的查看新车二手车  直营的只能做单一的类型
                    list =this.getBaseInfoByStatusOtherAll(dto);
                    return list;
                }
                if(RoleTypeDic.ALL_CAR.equals(dto.getOwnBusinessType())&&!RoleTypeDic.ALL_CAR.equals(dto.getBusinessType())){
                    // 说明 直营新车二手车都可以做   非直营只能做单一的类型
                    list =this.getBaseInfoByStatusOwnAll(dto);
                    return list;
                }
                if(!RoleTypeDic.ALL_CAR.equals(dto.getBusinessType())&&!RoleTypeDic.ALL_CAR.equals(dto.getOwnBusinessType())){
                    // 进入这里说明 非直营是单一类型  直营是单一类型
                    list =this.getBaseInfoByStatusOtherOnlyOwnOnly(dto);
                    return list;
                }

            }
            if((dto.getBusinessType()==null||dto.getBusinessType()=="")&&(dto.getOwnBusinessType()!=null&&dto.getOwnBusinessType()!="")){
                // 说明只有 直营   没有非直营权限
                if(RoleTypeDic.ALL_CAR.equals(dto.getOwnBusinessType())){
                    // s 说明直营的可以查看新车和二手车
                    list =this.getBaseInfoByStatusOnlyOwnAll(dto);
                    return list;
                }else {
                    // 说明直营的只有单一权限
                    list =this.getBaseInfoByStatusOnlyOwnType(dto);
                    return list;
                }

            }
            if((dto.getOwnBusinessType()==null&&dto.getOwnBusinessType()=="")&&(dto.getBusinessType()!=null&&dto.getBusinessType()!="")){
                // 说明只有非直营  没有 直营权限
                if(RoleTypeDic.ALL_CAR.equals(dto.getBusinessType())){
                    // 说明 非直营的新车二手车都能看
                    list =this.getBaseInfoByStatusOnlyOtherAll(dto);
                    return list;
                }else {
                    // 说明非直营的  只有单一属性
                    list =this.getBaseInfoByStatusOnlyOtherType(dto);
                    return list;
                }
            }
            return list;

    }
    @Override
    public BaseInfoTypeDTO getRole() {
        // 第一步我要知道登录者的角色属性  是属于直营还是非直营
        List<String> role = SecurityUtils.getRoles();
        BaseInfoTypeDTO dto =new BaseInfoTypeDTO();
        // 先判断类型
        if(role.contains(ChannelRoleEnum.ROLE_ADMIN.name())){
            // 说明进来的是管理员
            dto.setBusinessType(RoleTypeDic.ALL_CAR);
            dto.setOwnBusinessType(RoleTypeDic.ALL_CAR);

        }else {
            if(role.contains(ChannelRoleEnum.ROLE_OTHER_NEW_MANAGE_CHANNEL_DEVELOP.name())||role.contains(ChannelRoleEnum.ROLE_OTHER_NEW_SEE_CHANNEL_DEVELOP.name())){
                // 进入这个说明有非直营的新车角色
                dto.setBusinessType(RoleTypeDic.NEW_CAR);
            }
            if(role.contains(ChannelRoleEnum.ROLE_OTHER_OLD_MANAGE_CHANNEL_DEVELOP.name())||role.contains(ChannelRoleEnum.ROLE_OTHER_OLD_SEE_CHANNEL_DEVELOP.name())){
                if(RoleTypeDic.NEW_CAR.equals(dto.getBusinessType())){
                    // 说明我之前新车 又有二手车
                    dto.setBusinessType(RoleTypeDic.ALL_CAR);
                }else {
                    // 说明我只有二手车
                    dto.setBusinessType(RoleTypeDic.OLD_CAR);
                }
            }
            // 再去看看他有直营的角色吗
            if(role.contains(ChannelRoleEnum.ROLE_OWN_NEW_MANAGE_CHANNEL_DEVELOP.name())||role.contains(ChannelRoleEnum.ROLE_OWN_NEW_SEE_CHANNEL_DEVELOP.name())){
                dto.setOwnBusinessType(RoleTypeDic.NEW_CAR);
            }
            if(role.contains(ChannelRoleEnum.ROLE_OWN_OLD_MANAGE_CHANNEL_DEVELOP.name())||role.contains(ChannelRoleEnum.ROLE_OWN_OLD_SEE_CHANNEL_DEVELOP.name())){
                if(RoleTypeDic.NEW_CAR.equals(dto.getOwnBusinessType())){
                    // 说明我之前新车 又有二手车
                    dto.setOwnBusinessType(RoleTypeDic.ALL_CAR);
                }else {
                    // 说明我只有二手车
                    dto.setOwnBusinessType(RoleTypeDic.OLD_CAR);
                }
            }
        }
        return dto;
    }


    @Override
    public List<ChannelBaseInfoTemp> getBaseInfoByStatus(BaseInfoTypeDTO dto) {
        return this.baseMapper.getBaseInfoByStatus(dto);
    }

    @Override
    public List<ChannelBaseInfoTemp> getBaseInfoByStatusOtherAll(BaseInfoTypeDTO dto) {
        return this.baseMapper.getBaseInfoByStatusOtherAll(dto);
    }

    @Override
    public List<ChannelBaseInfoTemp> getBaseInfoByStatusOwnAll(BaseInfoTypeDTO dto) {
        return this.baseMapper.getBaseInfoByStatusOwnAll(dto);
    }

    @Override
    public List<ChannelBaseInfoTemp> getBaseInfoByStatusOnlyOwnAll(BaseInfoTypeDTO dto) {
        return this.baseMapper.getBaseInfoByStatusOnlyOwnAll(dto);
    }

    @Override
    public List<ChannelBaseInfoTemp> getBaseInfoByStatusOnlyOwnType(BaseInfoTypeDTO dto) {
        return this.baseMapper.getBaseInfoByStatusOnlyOwnType(dto);
    }

    @Override
    public List<ChannelBaseInfoTemp> getBaseInfoByStatusOnlyOtherAll(BaseInfoTypeDTO dto) {
        return this.baseMapper.getBaseInfoByStatusOnlyOtherAll(dto);
    }

    @Override
    public List<ChannelBaseInfoTemp> getBaseInfoByStatusOnlyOtherType(BaseInfoTypeDTO dto) {
        return this.baseMapper.getBaseInfoByStatusOnlyOtherType(dto);
    }

    @Override
    public List<ChannelBaseInfoTemp> getBaseInfoByStatusOtherOnlyOwnOnly(BaseInfoTypeDTO dto) {
        return this.baseMapper.getBaseInfoByStatusOtherOnlyOwnOnly(dto);
    }
    public IResponse send(VoucherFlowInfoDto dto) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", channelConfig.getVoucherClientId());
        requestHeader.put("clientSecret", channelConfig.getVoucherClientSecret());
        return voucherFlowApiFeign.send(dto, requestHeader);
    }

    @Override
    public IResponse getStopNewApply(ChannelUserCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", channelConfig.getApplyAdminClientId());
        requestHeader.put("clientSecret", channelConfig.getApplyAdminClientSecret());
        return applyFeignService.getStopNewApply(condition, requestHeader);
    }

    @Override
    public IResponse getStopOldApply(ChannelUserCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", channelConfig.getApplyAdminClientId());
        requestHeader.put("clientSecret", channelConfig.getApplyAdminClientSecret());
        return applyFeignService.getStopOldApply(condition, requestHeader);
    }

    @Override
    public IResponse getStopNewUser(ChannelUserCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", channelConfig.getApplyAdminClientId());
        requestHeader.put("clientSecret", channelConfig.getApplyAdminClientSecret());
        return applyFeignService.getStopNewUser(condition, requestHeader);
    }

    @Override
    public IResponse getStopOldUser(ChannelUserCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", channelConfig.getApplyAdminClientId());
        requestHeader.put("clientSecret", channelConfig.getApplyAdminClientSecret());
        return applyFeignService.getStopOldUser(condition, requestHeader);
    }

    @Override
    public IResponse stopApply(StartOrStopDTO condition) {
        //截取字符串
        String[] ids = condition.getIds().split(",");
        List<Long> caseList =new ArrayList<>();
        for (int j = 0; j < Arrays.asList(ids).size(); j++) {
            Long id = Long.valueOf(Arrays.asList(ids).get(j));
            caseList.add(id);
        }
        StartOrStop s =new StartOrStop();
        s.setChannelId(condition.getChannelId());
        s.setIds(caseList);
        s.setType(condition.getType());
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", channelConfig.getApplyAdminClientId());
        requestHeader.put("clientSecret", channelConfig.getApplyAdminClientSecret());
        return applyFeignService.stopApply(s, requestHeader);
    }

    @Override
    public IResponse startApply(StartOrStopDTO condition) {
        //截取字符串
        String[] ids = condition.getIds().split(",");
        List<Long> caseList =new ArrayList<>();
        for (int j = 0; j < Arrays.asList(ids).size(); j++) {
            Long id = Long.valueOf(Arrays.asList(ids).get(j));
            caseList.add(id);
        }
        StartOrStop s =new StartOrStop();
        s.setChannelId(condition.getChannelId());
        s.setIds(caseList);
        s.setType(condition.getType());
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", channelConfig.getApplyAdminClientId());
        requestHeader.put("clientSecret", channelConfig.getApplyAdminClientSecret());
        return applyFeignService.startApply(s, requestHeader);
    }


    @Override
    public IResponse stopUser(StartOrStopDTO condition) {
        //截取字符串
        String[] ids = condition.getIds().split(",");
        List<Long> caseList =new ArrayList<>();
        for (int j = 0; j < Arrays.asList(ids).size(); j++) {
            Long id = Long.valueOf(Arrays.asList(ids).get(j));
            caseList.add(id);
        }
        StartOrStop s =new StartOrStop();
        s.setChannelId(condition.getChannelId());
        s.setIds(caseList);
        s.setType(condition.getType());
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", channelConfig.getApplyAdminClientId());
        requestHeader.put("clientSecret", channelConfig.getApplyAdminClientSecret());
        return applyFeignService.stopUser(s, requestHeader);
    }


    @Override
    public IResponse startUser(StartOrStopDTO condition) {
        //截取字符串
        String[] ids = condition.getIds().split(",");
        List<Long> caseList =new ArrayList<>();
        for (int j = 0; j < Arrays.asList(ids).size(); j++) {
            Long id = Long.valueOf(Arrays.asList(ids).get(j));
            caseList.add(id);
        }
        StartOrStop s =new StartOrStop();
        s.setChannelId(condition.getChannelId());
        s.setIds(caseList);
        s.setType(condition.getType());
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", channelConfig.getApplyAdminClientId());
        requestHeader.put("clientSecret", channelConfig.getApplyAdminClientSecret());
        return applyFeignService.startUser(s, requestHeader);
    }

    @Override
    public JSONObject sendChannelId(ChannelBaseInfoTemp infoTemp) {
        StringBuffer xml = new StringBuffer();
        JSONObject json = new JSONObject();
        json.put("ID",infoTemp.getId().toString());
        json.put("TableCode","SUORG");
        if(VALUE_CHANNEL_STATUS_ZERO.equals(infoTemp.getChannelStatusOldCar()) || VALUE_CHANNEL_STATUS_ZERO.equals(infoTemp.getChannelStatus()) ){
            log.info("新车渠道状态"+infoTemp.getChannelStatus());
            log.info("二手车渠道状态"+infoTemp.getChannelStatusOldCar());
            xml.append("<DATA>");
            xml.append("</DATA>");
            json.put("Data",xml);
            //Action:2,修改
            json.put("Aciton","2");
        }else{
            log.info("新车渠道状态"+infoTemp.getChannelStatus());
            log.info("二手车渠道状态"+infoTemp.getChannelStatusOldCar());
            xml.append("<DATA>");
            xml.append("</DATA>");
            json.put("Data",xml);
            //Action:1,新车
            json.put("Aciton","1");
        }
        return json;
    }

    @Override
    public List<ChannelBulkEditingQuotaInfoVo> getByConditionForBulkEditing( ChannelOnlineCondition condition) {
        if(condition.getIds() == null || condition.getIds().length == 0){
            return baseMapper.getAllByConditionForBulkEditing(condition);
        }
        return baseMapper.getByConditionForBulkEditing(condition);
    }

    /**
     * 获取渠道基本信息列表
     * @param condition
     * @return
     */
    @Override
    public IResponse<ChannelBaseInfoTemp> getByCondition(ChannelOnlineCondition condition) {
        Page page = new Page(condition.getPageNumber(), condition.getPageSize());
        //  businessTypeChannel：01是新车  02 二手车  03是非直营新车二手车都有
        // ownBusinessTypeChannel ：01是新车  02 二手车  03  是直营新车二手车都有
        List<String> role = SecurityUtils.getRoles();
        //判断当前角色是否拥有新车区域经理或者二手车区域经理角色
        if(role.contains(ChannelRoleEnum.ROLE_NEW_REG_MANAGER.name())||role.contains(ChannelRoleEnum.ROLE_OLD_REG_MANAGER.name())){
            condition.setUserName(SecurityUtils.getUsername());
        }
        IPage<ChannelOnlineInfoVo> channelList = this.getChannelInfo(page, condition);
        for (ChannelOnlineInfoVo u : channelList.getRecords()) {
            List<WorkTaskDetail> detailList = workTaskDetailService.list(Wrappers.<WorkTaskDetail>query().lambda().eq(WorkTaskDetail::getBusinessKey, u.getId()));
            if(detailList !=null && detailList.size()>0){
                for(WorkTaskDetail detail :detailList){
                    WorkTaskAssign assgin = workTaskAssignService.getOne(Wrappers.<WorkTaskAssign>query().lambda().eq(WorkTaskAssign::getBizDataId, detail.getBizDataId()));
                    if(assgin !=null){
                        if(BUSINESS_TYPE_OLD_CAR.equals(detail.getBusinessType())){
                            u.setOwnerOld(assgin.getUserRealName());
                        }else if(BUSINESS_TYPE_NEW_CAR.equals(detail.getBusinessType())){
                            u.setOwnerNew(assgin.getUserRealName());
                        }
                    }
                }
            }
            WorkTaskApproveRecord recordNew =null;
            List<WorkTaskApproveRecord> recordList = workTaskApproveRecordService.list(Wrappers.<WorkTaskApproveRecord>query().lambda()
                    .eq(WorkTaskApproveRecord::getBusinessType, BUSINESS_TYPE_NEW_CAR)
                    .eq(WorkTaskApproveRecord::getApprovalOpinion,APPROVAL_OPINION_TWO)
                    .eq(WorkTaskApproveRecord::getBusinessKey,u.getId()).orderByDesc(WorkTaskApproveRecord::getCreateTime));
            if(recordList !=null && recordList.size()>0){
                recordNew = recordList.get(0);
                if(recordNew !=null){
                    u.setRejectNewRemark(recordNew.getRemark());
                }
            }

            WorkTaskApproveRecord recordOld =null;
            List<WorkTaskApproveRecord> recordOldList = workTaskApproveRecordService.list(Wrappers.<WorkTaskApproveRecord>query().lambda()
                    .eq(WorkTaskApproveRecord::getBusinessType, BUSINESS_TYPE_OLD_CAR)
                    .eq(WorkTaskApproveRecord::getApprovalOpinion,APPROVAL_OPINION_TWO)
                    .eq(WorkTaskApproveRecord::getBusinessKey,u.getId()).orderByDesc(WorkTaskApproveRecord::getCreateTime));
            if(recordOldList !=null && recordOldList.size()>0){
                recordOld = recordOldList.get(0);
                if(recordOld !=null){
                    u.setRejectOldRemark(recordOld.getRemark());
                }
            }

            ChannelMainBrand brand = channelMainBrandService.getOne(Wrappers.<ChannelMainBrand>query().lambda()
                    .eq(ChannelMainBrand::getChannelId, u.getId())
                    .eq(ChannelMainBrand::getCarLevel,CAR_LEVEL)
                    .eq(ChannelMainBrand::getIndeterminate,"false"));
            if(brand !=null){
                u.setMainBrand(ALL_BRAND);
            }
        }
        return IResponse.success(channelList);
    }
}
